package com.pureut.quality.controller;


import com.pureut.common.core.constant.Constants;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.quality.domain.dto.ExternalAuditPlanDto;
import com.pureut.quality.domain.dto.QualityBadCategoryDto;
import com.pureut.quality.domain.vo.ExternalAuditPlanVo;
import com.pureut.quality.domain.vo.QualityBadCategoryVo;
import com.pureut.quality.domain.vo.SubmitVo;
import com.pureut.quality.export.ExternalAuditPlanExport;
import com.pureut.quality.export.QualityBadCategoryExport;
import com.pureut.quality.service.ExternalAuditPlanService;
import com.pureut.quality.service.IQualityBadCategoryService;
import com.pureut.quality.util.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassDescription: 外审计划管理
 * @Author: YSL
 * @Created: 2025/4/8 10:19
 */
@RestController
@RequestMapping("/externalAuditPlan")
public class ExternalAuditPlanController extends BaseController {

    /**
     * 外审计划实现层
     */
    @Resource
    ExternalAuditPlanService externalAuditPlanService;

    /**
     * 外审计划列表
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:list")
    @GetMapping("/list")
    public TableDataInfo list(ExternalAuditPlanVo externalAuditPlanVo)
    {
        startPage();
        List<ExternalAuditPlanDto> externalAuditPlanDtos = externalAuditPlanService.listExternalAuditPlan(externalAuditPlanVo);
        return getDataTable(externalAuditPlanDtos);
    }

    /**
     * 根据id获取详细信息
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return AjaxResult.success(externalAuditPlanService.getInfo(id));
    }

    /**
     * 外审计划新增
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:add")
    @Log(title = "外审计划新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ExternalAuditPlanVo externalAuditPlanVo) throws Exception {
        return toAjax(externalAuditPlanService.insertExternalAuditPlan(externalAuditPlanVo));
    }

    /**
     * 外审计划修改
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:add")
    @Log(title = "外审计划修改", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ExternalAuditPlanVo externalAuditPlanVo) throws Exception {
        return toAjax(externalAuditPlanService.updateExternalAuditPlan(externalAuditPlanVo));
    }

    /**
     * 外审计划删除
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:remove")
    @Log(title = "外审计划删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") String ids){
        return toAjax(externalAuditPlanService.removeExternalAuditPlan(ids));
    }

    /**
     * 外审计划提交
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:add")
    @Log(title = "外审计划修改", businessType = BusinessType.INSERT)
    @PutMapping("/submit/{id}")
    public AjaxResult submit(@PathVariable("id") Long id)  {
        return toAjax(externalAuditPlanService.submitExternalAuditPlan(id));
    }

    /**
     * 外审计划审核
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:add")
    @Log(title = "外审计划审核", businessType = BusinessType.INSERT)
    @PutMapping("/examine")
    public AjaxResult examine(@Validated @RequestBody SubmitVo submitVo){
        return toAjax(externalAuditPlanService.examineExternalAuditPlan(submitVo));
    }

    /**
     * 外审计划撤销
     */
//    @RequiresPermissions("quality:verifyconfig:badcategory:add")
    @Log(title = "外审计划修改", businessType = BusinessType.INSERT)
    @PutMapping("/revoke/{id}")
    public AjaxResult revoke(@PathVariable("id") Long id) {
        return toAjax(externalAuditPlanService.revokeExternalAuditPlan(id));
    }

    /**
     * 生成外审计划管理导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String name = "外审计划管理";
        String fileName = Constants.STATIC_URL + "导入" + name + ".xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入外审计划管理.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 导出
     */
    @PostMapping("/selectAuditPlanExport")
    public AjaxResult selectAuditPlanExport(HttpServletResponse response, ExternalAuditPlanVo externalAuditPlanVo) throws IOException {
        List<ExternalAuditPlanExport> list = externalAuditPlanService.selectAuditPlanExport(externalAuditPlanVo);
        ExcelUtil<ExternalAuditPlanExport> excelUtil = new ExcelUtil<ExternalAuditPlanExport>(ExternalAuditPlanExport.class);
        excelUtil.excelWriter(response, list, "外审计划管理导出.xlsx");
        return AjaxResult.success();
    }

    /**
     * 外审计划管理导入
     */
    @PostMapping("/importAuditPlan")
    @Log(title = "外审计划管理导入", businessType = BusinessType.INSERT)
    public AjaxResult importAuditPlan(MultipartFile file) throws IOException {
        ExcelUtil<ExternalAuditPlanExport> excelUtil = new ExcelUtil<ExternalAuditPlanExport>(ExternalAuditPlanExport.class);
        List<ExternalAuditPlanExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(externalAuditPlanService.importAuditPlan(coCustomer));
    }

}
